//
//  RMOneWordPictureImageView.m
//  水印
//
//  Created by lanou3g on 16/4/23.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "RMOneWordPictureImageView.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface RMOneWordPictureImageView ()<UITextFieldDelegate,UIGestureRecognizerDelegate>


@end

@interface RMOneWordPictureImageView (){
    BOOL _isFinish;
}
@property (nonatomic, strong) UIImage *phImage;
@property (nonatomic, strong) UIImage *btImage;
@property(nonatomic,strong)UIImageView * photoImageView;
@property(nonatomic,strong)UIButton * deleteButton;

@end

@implementation RMOneWordPictureImageView
@synthesize wordTextField;

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self setWord];
//        UITapGestureRecognizer *imageViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapped)];
//        imageViewTap.delegate = self;
////        self.backgroundColor = [UIColor orangeColor];
//        [self addGestureRecognizer:imageViewTap];
//        [self setUserInteractionEnabled:YES];

        UIPanGestureRecognizer *drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wordTextField:)];
        [self addGestureRecognizer:drag];
    }
    return self;
}

-(void)setWord{
    wordTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,self.frame.size.height-30,200,30)];
    wordTextField.backgroundColor = [UIColor colorWithRed:212/255.0 green:101/255.0 blue:101/255.0 alpha:0.3];
    wordTextField.textColor = [UIColor whiteColor];
    wordTextField.alpha = 1;
    wordTextField.hidden = YES;
    wordTextField.tintColor = [UIColor cyanColor];
    wordTextField.delegate = self;
    wordTextField.placeholder = @"我可以动哦";
    wordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    wordTextField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    wordTextField.font = [UIFont systemFontOfSize:kRealValue(15)];
    [wordTextField addTarget:self action:@selector(changeInsert:) forControlEvents:(UIControlEventEditingChanged)];
    [self addSubview:wordTextField];
    
    

    

}

- (void)changeInsert:(UITextField *)textField{
    NSString *text = textField.text;
    CGRect textSize = [text boundingRectWithSize:CGSizeMake(100000, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kRealValue(15)]} context:nil];
    textField.bounds = CGRectMake(0, 0, textSize.size.width + 20, textField.bounds.size.height);
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    [self wordTextField];
}

- (void)imageViewTapped{
    
//    if (self.image != nil) {
//        
//        wordTextField.hidden = NO;
//    
//    
//    if(self.wordTextField.text.length > 20) {
//        // 超出字数限制（15字），弹出提示框
//        UIAlertView *alter = [[UIAlertView alloc] initWithTitle: @"输入文字太长" message: @"限制20个字" delegate: self cancelButtonTitle: @"确定" otherButtonTitles: nil, nil];
//        [alter show];
//    }
//    
//    if([wordTextField isFirstResponder]){
//        [wordTextField resignFirstResponder];
//        wordTextField.alpha = ([wordTextField.text isEqualToString:@""]) ? 0 : wordTextField.alpha;
//    } else {
//        [wordTextField becomeFirstResponder];
//        wordTextField.alpha = 1;
//    }
//        }
}

- (void)layoutSubviews{
    wordTextField.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    NSLog(@"%f,%f",self.center.x,self.center.y);
}
-(void)wordTextField:(UIGestureRecognizer * )gesture{

    CGPoint translation = [gesture locationInView:self];
    
    if(translation.y < wordTextField.frame.size.height/2){
        wordTextField.center = CGPointMake((kScreenWidth - 50) /2,  wordTextField.frame.size.height/2);
    
    } else if(self.frame.size.height < translation.y + wordTextField.frame.size.height/2){
        wordTextField.center = CGPointMake((kScreenWidth - 50)/2,  self.frame.size.height - wordTextField.frame.size.height/2);
    } else {
        wordTextField.center = CGPointMake((kScreenWidth - 50)/2,  translation.y);
    }

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [wordTextField resignFirstResponder];
    return true;
}



















@end
